package com.gs.common.tool.utils;

import com.gs.common.tool.pojo.OptionItem;

import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.util.Date;
import java.util.List;

/**
 * swing 工具类
 * @author: guos
 * @date: 2025/09/11 08:54
 **/
public class SwingUtil {


    /**
     * 获取颜色属性集对象
     * @param color    颜色类
     * @date 2025/09/11 08:55
     **/
    public static SimpleAttributeSet getAttributeSet(Color color) {
        SimpleAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setForeground(attr, color);
        return attr;
    }


    /**
     * 输出文本信息
     * @param doc    doc
     * @param attributeSet    颜色集
     * @param text    文本内容
     * @date 2025/09/11 08:59
     **/
    public static void writeRowText(StyledDocument doc, SimpleAttributeSet attributeSet, String text) {
        try {
            doc.insertString(doc.getLength(), text + "\n", attributeSet);
        } catch (BadLocationException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 输出蓝色文本信息
     * @param doc    doc
     * @param text    文本内容
     * @date 2025/09/11 08:59
     **/
    public static void writeRowBlueText(StyledDocument doc, String text) {
        try {
            SimpleAttributeSet blue = getAttributeSet(Color.BLUE);
            doc.insertString(doc.getLength(), text + "\n", blue);
        } catch (BadLocationException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 输出红色文本信息
     * @param doc    doc
     * @param text    文本内容
     * @date 2025/09/11 08:59
     **/
    public static void writeRowRedText(StyledDocument doc, String text) {
        try {
            SimpleAttributeSet blue = getAttributeSet(Color.RED);
            doc.insertString(doc.getLength(), text + "\n", blue);
        } catch (BadLocationException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 输出多行文本信息
     * @param doc    doc
     * @param textList    文本内容集合
     * @date 2025/09/11 08:59
     **/
    public static void writeRowsText(StyledDocument doc, List<String> textList) {
        SimpleAttributeSet green = getAttributeSet(Color.GREEN);
        SimpleAttributeSet blue = getAttributeSet(Color.BLUE);
        //输出日期
        writeRowText(doc, green, DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
        //划线
        writeRowText(doc, green, "--------------------------------------------------------------------------");
        //循环输出每行
        textList.forEach(r -> writeRowText(doc, blue, r));
        //划线
        writeRowText(doc, green, "--------------------------------------------------------------------------");
    }


    /**
     * 输出文本信息
     * @param doc    doc
     * @param errorMsg    错误信息
     * @date 2025/09/11 08:59
     **/
    public static void writeErrorMsg(StyledDocument doc, String errorMsg) {
        SimpleAttributeSet red = getAttributeSet(Color.RED);
        writeRowText(doc, red, errorMsg);
    }


    /**
     * 自定义组合框的呈现器
     * @param comboBox    呈现器对象
     * @date 2025/09/11 08:56
     **/
    public static void showComboBox(JComboBox<OptionItem> comboBox) {
        // 自定义组合框的呈现器
        comboBox.setRenderer(new BasicComboBoxRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                if (value instanceof OptionItem) {
                    OptionItem optionItem = (OptionItem) value;
                    value = optionItem.getValue();
                }
                //cellRendererComponent.setPreferredSize(new Dimension(30,20));
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }
        });
    }
}
